package com.action.proceso6;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.bean.proceso6.TipoContrato;
import com.conexion.DbConnection;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

//metodo para insertar TipoContrato 
public class TipoContratoAction {

	private final String TABLA = "tipocontrato";

	public int insertar(TipoContrato obj) {
		Connection conn = null;
		PreparedStatement pstm = null;
		try {

			conn = DbConnection.getConnection();

			String sql = "INSERT INTO " + TABLA + "(descripcion) VALUES(?)";
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, obj.getDescripcion());

			return pstm.executeUpdate();

		} catch (SQLException e) {
			Logger.getLogger(TipoContratoAction.class.getName()).log(
					Level.SEVERE, null, e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return -1;
	}

	// metodo para listar todo los tipos de contrato

	public ArrayList<TipoContrato> listar() {

		Connection conn = null;
		PreparedStatement pstm = null;

		ArrayList<TipoContrato> tipoContratos = new ArrayList<TipoContrato>();
		try {

			conn = DbConnection.getConnection();

			String sql = "Select * FROM " + TABLA;
			pstm = conn.prepareStatement(sql);

			ResultSet rs = pstm.executeQuery();

			while (rs.next()) {
				tipoContratos.add(new TipoContrato(rs.getInt(1), rs
						.getString(2)));
			}

		} catch (SQLException e) {
			Logger.getLogger(TipoContratoAction.class.getName()).log(
					Level.SEVERE, null, e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return tipoContratos;
	}

	// metodo para buscar el tipo conrato

	public TipoContrato buscar(String descripcion) {

		Connection conn = null;
		PreparedStatement pstm = null;
		TipoContrato tc = null;
		try {

			conn = DbConnection.getConnection();

			String sql = "Select * FROM " + TABLA + " where descripcion=?";
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, descripcion);

			ResultSet rs = pstm.executeQuery();

			if (rs.next())
				tc = new TipoContrato(rs.getInt(1), rs.getString(2));

		} catch (SQLException e) {
			Logger.getLogger(TipoContratoAction.class.getName()).log(
					Level.SEVERE, null, e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return tc;
	}

	public TipoContrato buscar(int id) {

		Connection conn = null;
		PreparedStatement pstm = null;
		TipoContrato tc = null;
		try {

			conn = DbConnection.getConnection();

			String sql = "Select * FROM " + TABLA + " where id=?";
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, id);

			ResultSet rs = pstm.executeQuery();

			if (rs.next())
				tc = new TipoContrato(rs.getInt(1), rs.getString(2));

		} catch (SQLException e) {
			Logger.getLogger(TipoContratoAction.class.getName()).log(
					Level.SEVERE, null, e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return tc;
	}

	// metodo para actualizar el tipo contrarto

	public int actualizar(TipoContrato obj) {
		Connection conn = null;
		PreparedStatement pstm = null;
		try {

			conn = DbConnection.getConnection();

			String sql = "Update " + TABLA + " set descripcion=? where id=?";
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, obj.getDescripcion());
			pstm.setInt(2, obj.getId());
			return pstm.executeUpdate();

		} catch (SQLException e) {
			Logger.getLogger(TipoContratoAction.class.getName()).log(
					Level.SEVERE, null, e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return -1;
	}

	// metodo para eliminar el tipo contrato

	public int eliminar(String descripcion) {
		Connection conn = null;
		PreparedStatement pstm = null;
		try {

			conn = DbConnection.getConnection();

			String sql = "delete from " + TABLA + " where descripcion=?";
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, descripcion);

			return pstm.executeUpdate();

		} catch (SQLException e) {
			Logger.getLogger(TipoContratoAction.class.getName()).log(
					Level.SEVERE, null, e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return -1;
	}

	public int eliminar(int id) {
		Connection conn = null;
		PreparedStatement pstm = null;
		try {

			conn = DbConnection.getConnection();

			String sql = "delete from " + TABLA + " where id=?";
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, id);

			return pstm.executeUpdate();

		} catch (SQLException e) {
			Logger.getLogger(TipoContratoAction.class.getName()).log(
					Level.SEVERE, null, e);
		} finally {
			try {
				if (pstm != null)
					pstm.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return -1;
	}

}
